#include "newfriendlist.h"
#include "ui_newfriendlist.h"

newfriendlist::newfriendlist(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::newfriendlist)
{
    ui->setupUi(this);
    timer_renewed->start();
    timer_addfriend->start();
    connect(timer_renewed,&QTimer::timeout,this,[=](){
        if(renewed)
        {
            ui->listWidget->clear();
            for(int i=0;i<nextnewfrientrow;i++)
            {
                QString account=newfriendsbyrow[i];
                newfrienditem* item=newfriends[account];
                newfrienditem* rep_item=new newfrienditem(ui->listWidget);
                rep_item->init(item->account,item->image,item->name,item->extrames);
                QListWidgetItem* li=new QListWidgetItem(ui->listWidget);
                li->setText(QString::number(i));
                li->setSizeHint(QSize(ui->listWidget->width()-25,50));
                ui->listWidget->setItemWidget(li,rep_item);
            }
        }
        renewed=false;
    });
    connect(timer_addfriend,&QTimer::timeout,this,[=](){
        if(renewed)   //避免在需要更新列表时对列表进行重复更新
            return;
        else
        {
            for(int i=0;i<nextnewfrientrow;i++)
            {
                QString account=newfriendsbyrow[i];
                newfrienditem* item=newfriends[account];
                if(item->confirmaddinfo)
                {
                    //告诉主界面widget添加的结果
                    emit addfriendresult(item->addfriend,item->account,item->image,item->name);
                    //更新列表
                    for(int j=i;j<nextnewfrientrow-1;j++)
                    {
                        QString nextaccount=newfriendsbyrow[j+1];
                        newfriendsbyrow[j]=nextaccount;
                    }
                    newfriendsbyrow.remove(nextnewfrientrow-1);
                    newfriends.remove(account);
                    nextnewfrientrow--;
                    QListWidgetItem* deleteitem=ui->listWidget->takeItem(i); //takeitem接口需要手动析构
                    if(deleteitem)
                        delete deleteitem;
                    break;
                }
            }
        }
    });
}

newfriendlist::~newfriendlist()
{
    delete ui;
}

void newfriendlist::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    int row=item->text().toInt();
    qDebug()<<"doubleclicklistwidget "<<row;
    if(newfriendsbyrow.find(row)!=newfriendsbyrow.end())
    {
        QString curaccount=newfriendsbyrow[row];
        newfrienditem* curitem=newfriends[curaccount];
        if(curitem!=nullptr)
        {
            if(curitem->pai!=nullptr)
            {
                curitem->pai->show();
            }
            else
            {
                qDebug()<<"newfriendlist50--curitem->pai is nullptr";
            }
        }
        else
        {
            qDebug()<<"newfriendlist48--curitem is nullptr";
        }
    }
}

